home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5150 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  59 lines

  1. Path: frco.com!usenet
  2. From: Jadam@tcmail.frco.com (Jim Adam)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help!! I'm a stuck newbie - input problem
  5. Date: 2 Feb 1996 19:30:05 GMT
  6. Organization: Fisher Rosemount Systems
  7. Distribution: world
  8. Message-ID: <4etont$6gn@rolaids.frco.com>
  9. References: <4eh3vl$g8i@atlas.uniserve.com>
  10. NNTP-Posting-Host: primrose.frco.com
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.93.11
  13.  
  14. In article <4eh3vl$g8i@atlas.uniserve.com>, nowher@anyplace.com says...
  15.  
  16. >Presently, I have the following variables declared:
  17.  
  18. >char TempStr[20];
  19. >char ch;
  20. >int x=0;
  21.  
  22. >I need to input a string with a max length of 15 chars.
  23. >( I need to fill the database fields:  Part Number:[               ]  )
  24.  
  25. >I do not want to overwrite the bracket on the right of the field. I have 
  26. >tried various methods, but I get errors, or it otherwise doesn't do what 
  27. >I need it to.  
  28.  
  29. Chris,
  30.  
  31. Can you explain your problem some more?  I.e., are you having trouble
  32. reading the string (from a file?  from standard input?) or are
  33. you having trouble outputting it (to a string? to the screen?).
  34.  
  35. Anyway, here's one approach using the standard input and output
  36. streams.  This assumes the part number your getting on a line by itself.
  37.  
  38.   void getPartNumber( istream& istr, char * buffer, size_t bufSize )
  39.   {
  40.     istr.getline( buffer, bufSize );
  41.   }
  42.  
  43.   void writePartNumber( ostream & ostr, const char * number )
  44.   {
  45.     char oldFill = ostr.fill( ' ' );
  46.     ostr << "Part Number:[" << setw(15) << number << ']' << endl;
  47.     ostr.fill( oldFill );
  48.   }
  49.  
  50.   void main( void )
  51.   {
  52.     char buffer[20];
  53.     getPartNumber( cin, buffer, sizeof(buffer));
  54.     writePartNumber( cout, buffer );
  55.   }
  56.  
  57. Jim
  58.  
  59.